var restify = require('restify');
var client = restify.createJsonClient({
	url : 'http://localhost:8060'
});
var i;
var tp = require('./addInfo');
var fs = require('fs');

exports.testPost = function(path, params) {
	client.post(path, params, function(err, req, res, data) {
		if (err) {
			console.log('testPost' + err);
		} else {
			//console.log(data);
		}
	});
}

exports.testGet = function() {
	client.get('/foo/luoxuan', function(err, req, res, data) {
		console.log(data);
	})
}

exports.carMoves = function() {

}
exports.addInfo = function(identity, num){
	if(identity == 'driver'){
		var i;
		for(i = 0; i < num; i++){
			var document = {};
			document.addInfo = {};
			document.kind = 'driver';
			document.addInfo.dri_id = "139123456" + i;
			document.addInfo.dri_credit = Math.floor(Math.random() * 5 + 1);
			
			document.addInfo.dri_lat = 31.195476 + Math.random()*0.06 - 0.03,
			document.addInfo.dri_lng = 121.54753 + Math.random()*0.08 - 0.04,
			
			document.addInfo.dri_pwd = '123',
			document.addInfo.taxi_number = 'SA123'+ Math.floor(Math.random()*10),
			document.addInfo.liscense = 'scd23123' + i,
			document.addInfo.car_color = 'red',
			document.addInfo.dri_nickname = 'luoxuan' + i,
			document.addInfo.dri_photo = 'kkk',
			document.addInfo.dri_sex = 'male',
			document.addInfo.dri_phone = "139123456" + i;
			/*
			 * addInfo : {kind : 'd', addInfo : { 'dri_id' : '13812345678', ...... }}
			 */
			tp.testPost('/addInfo' , document);
		}	
	}
	else if(identity == 'customer'){
		
	}else if(identity == 'order'){
		
	}	
}
/*
 * in : identity = 'driver' or 'order', num : update drivers number , maxi:maxi
 * update data number out : NULL
 */
exports.updateInfo = function(identity, num, maxi){
	if(identity == 'driver'){
		var dri_id = new Array;
		var arrpos = {};
		//construct dri_id array for update!
		for(var i = 0; i < num; i++){
			dri_id[i] = '139123456' + i;
		}
   	    arrpos = tp.getArrpos(dri_id);   	    
   	    //update info
   	    var t = 0;
   	    var interVal = setInterval(function(){
			var updatInfo = {};
			updatInfo.kind = 'd';
			for(var i=0;i<num;i++){
				updatInfo._id = dri_id[i];
				updatInfo.dri_lat = parseFloat(arrpos[dri_id[i]].lat[t]);
				updatInfo.dri_lng = parseFloat(arrpos[dri_id[i]].lng[t]);
				//console.log(updatInfo);
				tp.testPost('/update', updatInfo);
			}			
			t++;
			//update number for each driver
		    if(t >= maxi){
		    	 clearInterval(interVal);
   			    }
   			},3000);
	}
}
/*
 * in : dri_id = array; out : arrpos = {'1398000' :
 * {lat:[..,..],lng:[..,..]},.....}
 */
exports.getArrpos = function(dri_id){
	var arrpos = {};
	var i = 0;

	var interval = setInterval(function(){
		fs.readFile('./management/text/' + dri_id[i] + '.txt','utf8', function(err, data){
			if(err){
				console.log(err);
			}
			else{
				arrpos[dri_id[i]] = {};
				arrpos[dri_id[i]].lat = new Array;
				arrpos[dri_id[i]].lng = new Array;
				 
				arrpos[dri_id[i]].lat = tp.splitTextToArr(data, 'lat');
				arrpos[dri_id[i]].lng = tp.splitTextToArr(data, 'lng');
				i++;
				if(i >= dri_id.length){
					clearInterval(interval)
				}
			}
		});
		
	},10)
	return arrpos;
}
/*
 * in : strings = String; latLng = 'lat'; out : lat = array or lng = array;
 */
exports.splitTextToArr = function(strings, latLng){
	var arr = new Array;
	var lat = new Array;
	var lng = new Array;
	
	var re = /[\s,]/;
	var arr = strings.split(re);
	for(var i = 0; i < arr.length/6000-1; i++){
		lat[i] = arr[i*6 + 4];
		lng[i] = arr[i*6 + 3];
	}
	if(latLng === 'lat'){
		return lat;
	}
	else{
		return lng;
	}
}